package com.yw.wanandroid.widget.refresh

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.scwang.smart.refresh.layout.simple.SimpleComponent
import com.yw.wanandroid.R

class CommonRefreshHeard : SimpleComponent, RefreshHeader {
    val REFRESH_HEADER_PULLING = "下拉刷新"
    val REFRESH_HEADER_REFRESHING = "正在刷新"
    val REFRESH_HEADER_RELEASE = "释放刷新"
    val REFRESH_HEADER_FINISH = "刷新完成"
    val REFRESH_HEADER_FAILED = "刷新失败"
    val textTv: TextView

    constructor(wrapped: Context) : this(
        wrapped, null
    )

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs, 0) {
        View.inflate(context, R.layout.heard_refresh, this)
        textTv = findViewById(R.id.tv_time)
    }


    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
    }

    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        if (success) {
            textTv.text = REFRESH_HEADER_FINISH
        } else {
            textTv.text = REFRESH_HEADER_FAILED
        }
        return 200
    }

    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
        super.onStateChanged(refreshLayout, oldState, newState)
        when (newState) {
            RefreshState.None -> {
            }
            RefreshState.PullDownToRefresh -> {
                textTv.text = REFRESH_HEADER_PULLING
            }
            RefreshState.ReleaseToRefresh -> {
                textTv.text = REFRESH_HEADER_RELEASE
            }
            RefreshState.RefreshReleased -> {
                textTv.text = REFRESH_HEADER_REFRESHING
            }
            RefreshState.Refreshing -> {
            }
            RefreshState.RefreshFinish -> {
            }
        }
    }
}